const app = getApp();
var request = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    task: {
      taskId: ''
    },
    taskDetail: {},
    baseUrl: '',
    imgwidth: 0,
    imgheight: 0
  },

  onLoad: function (options) {
    var that = this
    var url = wx.getStorageSync('baseUrl')
    that.setData({
      baseUrl: url
    })
    that.data.task.taskId = options.taskId;
    if (options.taskId != null) {
      request.post("/detail/showTaskDetail", that.data.task, function (res) {
        if (res.code == 200) {
          console.log(res.data)
          that.setData({
            taskDetail: res.data
          })
        } else {
          wx.showToast({
            title: '任务信息加载失败',
            icon: 'none',
            duration: 1000
          });
        }
      })
    }
  },

  takeTask: function () {
    var that = this
    var loginName = that.data.taskDetail.schoolUser.loginName;
    var userName = that.data.taskDetail.schoolUser.userName;
    var avatar = that.data.taskDetail.schoolUser.avatar;
    if (avatar == '') {
      avatar = '../../static/images/logo.png'
    }
    var fromUser = wx.getStorageSync('userInfo');
    var chatList = {
      chatFromLoginName: fromUser.loginName,
      chatFromUserName: fromUser.userName,
      fromUserAvatar: fromUser.avatar,
      chatToLoginName: loginName,
      chatToUserName: userName,
      toUserAvatar: avatar
    }
    console.log(chatList)
    if (loginName != '' && userName != '') {
      //将信息添加道消息列表
      request.post('/chatList/addChatList', chatList, function (res) {
        console.log(res);
      })
      //传送至聊天页面
      wx.navigateTo({
        url: '/pages/chat/chat?loginName=' + loginName + '&userName=' + userName
      })
    } else {
      wx.showToast({
        title: '聊天窗口无法创建！！！',
        icon: 'none',
        duration: 1000
      });
    }
  },
  imageLoad: function (e) {
    let that = this;
    let width = e.detail.width, //获取图片真实宽度
      height = e.detail.height,
      ratio = width / height; //图片的真实宽高比例
    let viewWidth = 784, //设置图片显示宽度，
      viewHeight = 784 / ratio; //计算的高度值
      that.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  }
})